package cn.com.taiji.oospub.wechat.service.impl;

import cn.com.taiji.oospub.wechat.entity.CourseNutrition;
import cn.com.taiji.oospub.wechat.entity.Courses;
import cn.com.taiji.oospub.wechat.mapper.CourseNutritionMapper;
import cn.com.taiji.oospub.wechat.mapper.CoursesMapper;
import cn.com.taiji.oospub.wechat.service.ICoursesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.dreamyoung.mprelation.AutoMapper;
import com.github.dreamyoung.mprelation.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-10-30
 */
@Service
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements ICoursesService {
    @Autowired
    private CoursesMapper coursesMapper;
    @Autowired
    private CourseNutritionMapper courseNutritionMapper;
    @Autowired
    private AutoMapper autoMapper;

    @Override
    public List<String> queryDistinctTian(QueryWrapper<Courses> queryModel) {
        queryModel.select("distinct dateStr");
        List<String> dateStr = new ArrayList<String>((List) coursesMapper.selectObjs(queryModel));
        return dateStr;
    }

    @Override
    public List<String> queryDistinctMealId(QueryWrapper<Courses> queryModel) {
        queryModel.select("distinct meal_time_code");
        List<String> mealsTimeCode = new ArrayList<String>((List) coursesMapper.selectObjs(queryModel));
        return mealsTimeCode;
    }

    @Override
    public Courses findById(String courseId) {
        QueryWrapper<CourseNutrition> queryWrapper = new QueryWrapper<>();
        Courses courses = coursesMapper.selectById(courseId);
        queryWrapper.eq("recipes_code", courses.getPosNo());
        queryWrapper.eq("is_delete", false);
        queryWrapper.in("nutrition_id", 4, 13, 7, 5, 6, 2);
        courses.setCnList(courseNutritionMapper.selectList(queryWrapper));
        return courses;
    }

    @Override
    public List<Courses> listdayMenu(String mealTimeCode, String schoolId, String dateNum, String lb, String restaurantId) {
        QueryWrapper<Courses> queryWrapper = new QueryWrapper<>();
        //周一时间
        LocalDate today = LocalDate.now();
        LocalDate searchDate = today.plusDays(Integer.parseInt(dateNum) - 1);
        queryWrapper.ge("startTime", LocalDateTime.of(searchDate, LocalTime.MIN));
        queryWrapper.le("endTime", LocalDateTime.of(searchDate, LocalTime.MAX).minusHours(1));
        queryWrapper.eq("school_id", schoolId);
        queryWrapper.eq("is_stop", "0");
//        queryWrapper.eq("restaurant_id", restaurantId);
        if (lb != null) {
            queryWrapper.in("type", lb, "dcwm");
        }
        if (mealTimeCode != null && !"".equals(mealTimeCode)) {
            queryWrapper.eq("meal_time_code", mealTimeCode);
        }
        queryWrapper.orderByAsc("name_ch");
        List<Courses> courses = coursesMapper.selectList(queryWrapper);
        for (Courses course : courses) {
            QueryWrapper<CourseNutrition> courseNutrition = new QueryWrapper<>();
            courseNutrition.eq("is_delete", false);
            courseNutrition.in("nutrition_id", 4, 13, 7, 5, 6, 2);
            courseNutrition.eq("recipes_code", course.getPosNo());
            course.setCnList(courseNutritionMapper.selectList(courseNutrition));
        }
        return courses;
    }

    @Override
    public List<Courses> listTopCourse(String menuTimeCode, String schoolId, String dateNum, String lb, String restaurantId) {
        QueryWrapper<Courses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_top", "1");
        queryWrapper.eq("is_delete", "0");
        queryWrapper.eq("is_stop", "0");
        queryWrapper.eq("school_id", schoolId);
//        queryWrapper.eq("restaurant_id", restaurantId);
        if (lb != null) {
            queryWrapper.in("type", lb, "dcwm");
        }
        if (menuTimeCode != null && !"".equals(menuTimeCode)) {
            queryWrapper.eq("meal_time_code", menuTimeCode);
        }
        LocalDate now = LocalDate.now();
        if ("0".equals(dateNum)) {
            //查询今日推荐菜品
            queryWrapper.ge("startTime", LocalDateTime.of(now, LocalTime.MIN));
            queryWrapper.le("endTime", LocalDateTime.of(LocalDate.now(), LocalTime.MAX).minusMinutes(1));
        } else if ("1".equals(dateNum)) {
            //查询明日推荐菜品
            queryWrapper.ge("startTime", LocalDateTime.of(now, LocalTime.MIN).plusDays(1));
            queryWrapper.le("endTime", LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MAX).minusMinutes(1));
        }
        List<Courses> courses = coursesMapper.selectList(queryWrapper);
        for (Courses course : courses) {
            QueryWrapper<CourseNutrition> courseNutrition = new QueryWrapper<>();
            courseNutrition.eq("is_delete", false);
            courseNutrition.in("nutrition_id", 4, 13, 7, 5, 6, 2);
            courseNutrition.eq("recipes_code", course.getPosNo());
            List<CourseNutrition> courseNutritions = courseNutritionMapper.selectList(courseNutrition);
            course.setCnList(courseNutritions);
        }
        return courses;
    }

    @Override
    public Courses findByProperty(String coulumName, String value) {
        QueryWrapper<Courses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coulumName, value);
        List<Courses> courses = coursesMapper.selectList(queryWrapper);
        if (courses != null && courses.size() > 0) {
            return autoMapper.mapperEntity(courses.get(0));
        } else {
            return null;
        }
    }

    @Override
    public Boolean clearCourses(String mealCode, LocalDate localDate) {
        UpdateWrapper<Courses> coursesUpdateWrapper = new UpdateWrapper<>();
        coursesUpdateWrapper.ge("startTime", localDate);
        coursesUpdateWrapper.le("endTime", LocalDateTime.of(localDate, LocalTime.MAX));
        if (StringUtils.hasText(mealCode)) {
            coursesUpdateWrapper.eq("meal_time_code", mealCode);
        }
        coursesUpdateWrapper.set("remain_count", 0);
        return this.update(coursesUpdateWrapper);
    }

}
